package org.devocean.groupbuying.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.devocean.groupbuying.dao.ContactDAO;
import org.devocean.groupbuying.domain.Contact;
import org.springframework.stereotype.Repository;

@Repository
public class ContactDAOImpl implements ContactDAO {
	@PersistenceContext
	private EntityManager em;

	
	public void persiste(Contact contact) {
		em.persist(contact);

	}

	public List<Contact> listContact() {
		return em.createQuery("select c from Contact c").getResultList();
	}

	public void removeContact(Integer id) {
		final Contact contact = em.find(Contact.class, id);
		if (contact != null) {
			em.remove(contact);
		}

	}

	public Contact findById(Integer id) {
		return em.find(Contact.class, id);
	}

	public Contact updateContact(Contact contact) {
		final Contact merged = em.merge(contact);
		em.flush();
		return merged;
	}
}
